﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using internal_website.Models;

namespace internal_website.CustomHtmlHelper
{
    public static class ExtendedTextBoxFor
    {
        public static MvcHtmlString MyTextBoxFor<TModel, TProperty>(this HtmlHelper<TModel> helper, 
                                                                    Expression<Func<TModel, TProperty>> expression)
        {
            /*http://stackoverflow.com/questions/5336920/modelmetadata-for-complex-type-in-editortemplate-in-asp-net-mvc*/
            var metadata = ModelMetadata.FromLambdaExpression(expression, helper.ViewData); 
            var watermark = metadata.Watermark;
            return helper.TextBoxFor(expression, new {@class = "txt"});
        }

        public static MvcHtmlString MyTextBoxFor<TModel, TProperty>(this HtmlHelper<TModel> helper,
                                                                    Expression<Func<TModel, TProperty>> expression,
                                                                    object htmlAttributes)
        {

            //HtmlHelper.AnonymousObjectToHtmlAttributes returns RouteValueDictionary. You can add value to that dictionary, you do not need to add property to anonymous object.
            var attrs = HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes);
            var metadata = ModelMetadata.FromLambdaExpression(expression, helper.ViewData);
            var watermark = metadata.Watermark;

            if (watermark != null)
            {
                attrs.Add("placeholder", watermark);
            }

            return helper.TextBoxFor(expression, attrs);
        }

        public static MvcHtmlString MyTextAreaFor<TModel, TProperty>(this HtmlHelper<TModel> helper,
                                                                    Expression<Func<TModel, TProperty>> expression)
        {
            /*http://stackoverflow.com/questions/5336920/modelmetadata-for-complex-type-in-editortemplate-in-asp-net-mvc*/
            var metadata = ModelMetadata.FromLambdaExpression(expression, helper.ViewData);
            var watermark = metadata.Watermark;
            return helper.TextAreaFor(expression, new { @class = "txt" });
        }

        public static MvcHtmlString MyTextAreaFor<TModel, TProperty>(this HtmlHelper<TModel> helper,
                                                                    Expression<Func<TModel, TProperty>> expression,
                                                                    object htmlAttributes)
        {

            //HtmlHelper.AnonymousObjectToHtmlAttributes returns RouteValueDictionary. You can add value to that dictionary, you do not need to add property to anonymous object.
            var attrs = HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes);
            var metadata = ModelMetadata.FromLambdaExpression(expression, helper.ViewData);
            var watermark = metadata.Watermark;

            if (watermark != null)
            {
                attrs.Add("placeholder", watermark);
            }

            return helper.TextAreaFor(expression, attrs);
        }
    }
}